<?php
/**
 * Created by PhpStorm.
 * User: 96572
 * Date: 2017/5/14
 * Time: 15:56
 */
use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $model app\modules\admin\models\User */
/* @var $roles \yii\rbac\Role */
$view = $this;
\application\assets\LayUIAsset::register($this);
function renderPermissionItem($menus,yii\web\View $view,$rolePermissions){
    $html = '';
    foreach ($menus as $index => $item) {
        $tempHtml = '';
        if ($item['menu_is_lowest'] == 0) {
            $child = renderPermissionItem($item['children'],$view,$rolePermissions);
            $tempHtml = $view->render(
                    'permission-item',
                    [
                            'menu'=>$item,
                            'child'=>$child,
                            'rolePermissions'=>$rolePermissions,
                    ]
            );
        } else {
            $tempHtml = $view->render(
                    'permission-item',
                    [
                        'menu'=>$item,
                        'child'=>'',
                        'rolePermissions'=>$rolePermissions,
                    ]
            );
        }
        $html .= $tempHtml;
    }
    return $html;
}
?>
<style>
    .layui-form-switch{width: 52px;}
</style>
<div class="menu-list">
    <h1><?= Html::encode($this->title) ?></h1>
    <div class="user-form">
        <?php $form = ActiveForm::begin(['options'=>['class' => 'layui-form']]); ?>
        <div class="container">
            <div class="row">
                <fieldset class="layui-elem-field">
                    <legend></legend>
                    <div class="row layui-field-box" style="margin-left: 2px;">
                        <?=renderPermissionItem($menus,$this,$rolePermissions)?>
                    </div>
                </fieldset>
            </div>
        </div>
        <div class="form-group">
            <button type="submit" class="btn btn-success">Update</button>
        </div>
        <?php foreach ($rolePermissions as $index => $rolePermission) { ?>
            <input type="hidden" name="original_name[]" value="<?=$rolePermission?>">
        <?php } ?>
        <?php ActiveForm::end(); ?>
    </div>
</div>
<script>
    window.onload = function () {
        //主动加载jquery模块
        layui.use(['jquery', 'layer','form'], function(){
            var $ = layui.jquery //重点处
                ,layer = layui.layer;
            var form = layui.form(); //只有执行了这一步，部分表单元素才会修饰成功
        });
    };
</script>